﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Casemeet.Pages;
using VK.Comm;
using VK.Comm.TransactSql;
using System.Data;
using VK.Casemeet.Web.Admin;

namespace VK.Casemeet.Web.Admin.Pages
{
    public partial class CustomPageEdit : System.Web.UI.Page
    {
        public int pageID = 0;
        public string content = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            (this.Master as Site).SiteMapPathControl.MapPathList = new List<MapPath>(){
                new MapPath{ Text="桌面",Url="/Admin/Home.aspx"},
                new MapPath{ Text="页面编辑",Url="#"}
            };

            pageID = Util.GetQueryInt("PageID");

            if (!IsPostBack)
            {
                BindPage();
            }
        }

        internal void BindPage()
        {
            if (pageID != 0)
            {
                //编辑时
                CustomPageEntity customPage = CustomPageBLL.GetCustomPage(pageID);
                txtTitle.Text = customPage.Title;
                labType.Text = CustomPageHelper.GetTypeLabel(customPage.Type);
                content = Server.HtmlDecode(customPage.Content);
                chkIsEnable.Checked = customPage.IsEnable;
            }
            else
            {
                labType.Text = CustomPageHelper.GetTypeLabel(2);
            }
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            if (pageID != 0)
            {
                #region Edit
                CustomPageEntity customPage = LoadEntity();
                customPage.PageID = pageID;

                if (CustomPageBLL.UpdateCustomPage(customPage) > 0)
                {
                    Message.Msg(this.Page, "保存成功!", MessageType.Success);
                }
                else
                {
                    Message.Msg(this.Page, "保存失败!", MessageType.Failure);
                }
                #endregion
            }
            else
            {
                #region Add
                CustomPageEntity customPage = LoadEntity();
                customPage.Type = 2;

                if (CustomPageBLL.InsertCustomPage(customPage) > 0)
                {
                    Message.Msg(this.Page, "保存成功!", MessageType.Success);
                }
                else
                {
                    Message.Msg(this.Page, "保存失败!", MessageType.Failure);
                }
                #endregion
            }
        }

        protected CustomPageEntity LoadEntity()
        {
            CustomPageEntity customPage = new CustomPageEntity();
            customPage.Title = txtTitle.Text;
            customPage.Content = Server.HtmlEncode(Util.GetFormString("txtContent"));
            customPage.IsEnable = chkIsEnable.Checked;
            return customPage;
        }
    }
}